package com.algorithm;

public class LeftBound {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 5, 6};
        System.out.println(left_bound(arr, -1));
    }

    public static int left_bound(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            } else if (nums[mid] > target) {
                right = mid - 1;
            } else if (nums[mid] == target) {
                right = mid - 1;
            }
        }
        if (left >= nums.length) {
            return -1;
        }
        if (nums[left] == target) {
            return left;
        }
        return -1;
    }
}
